/*
 * Created on Nov 20, 2003
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package tools.blat;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


/**
 * @author cm	
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 *
 */

public class PslParser {
	private BufferedReader reader;
	private boolean hasmoreseq= true;
	private EstAlignment curralign= null;

	public PslParser(BufferedReader reader) throws IOException, Exception {
		this.reader= reader;
		
		String s= reader.readLine();
		if (!s.equals("psLayout version 3"))
			throw new Exception("Unexcepted line: "+s);

		s= reader.readLine();
		if (!s.equals(""))
				throw new Exception("Unexcepted line: "+s);
		
		s= reader.readLine();
		s= reader.readLine();
		s= reader.readLine();
		readNext();
	}
	
	/* (non-Javadoc)
	 * @see java.util.Iterator#hasNext()
	 */
	public boolean hasNext() {
		return hasmoreseq;
	}

	/* (non-Javadoc)
	 * @see java.util.Iterator#next()
	 */
	public Object next() {
		try {
			return nextAlignment();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @return
	 */
	public boolean hasMoreAlignments() {
		return hasmoreseq;
	}
	
	/**
	 * 
	 * @return
	 * @throws IOException
	 * @throws BioException
	 */
	public EstAlignment nextAlignment() throws IOException, Exception {
		if (hasmoreseq == false) 
			throw new Exception("No more sequences");
		EstAlignment tmp= curralign;
		readNext();
		
		return tmp;
	}
	
	private void readNext() throws IOException, Exception {
		String s= reader.readLine();
		if (s==null) {
			hasmoreseq= false;
		}
		else {
			String[] l= s.split("\t");
			if (l.length != 21) {
				throw new Exception("Invalid line: "+s);
			}
			
			EstAlignment align= new EstAlignment();
			align.matches= Integer.parseInt(l[0]);		
			align.mismatches= Integer.parseInt(l[1]);
			align.repmatches= Integer.parseInt(l[2]);
			align.ncount= Integer.parseInt(l[3]);
			align.qnuminsert= Integer.parseInt(l[4]);
			align.qbaseinsert= Integer.parseInt(l[5]);
			align.tnuminsert= Integer.parseInt(l[6]);
			align.tbaseinsert= Integer.parseInt(l[7]);
			
			
				
			align.qname= l[9];
			align.qsize= Integer.parseInt(l[10]);
			align.qstart= Integer.parseInt(l[11]);
			align.qend= Integer.parseInt(l[12]);
			align.tname= l[13];
			align.tsize= Integer.parseInt(l[14]);
			align.tstart= Integer.parseInt(l[15]);
			align.tend= Integer.parseInt(l[16]);
			
			
			String[] blockSizes = l[18].split(",");
			String[] qStarts = l[19].split(",");
			String[] tStarts = l[20].split(",");
			
			int blockCount = blockSizes.length;
			align.blockSizes = new int[blockCount];
			align.qStarts = new int[blockCount];
			align.tStarts = new int[blockCount];
			for(int j=0;j < blockSizes.length;j++) align.blockSizes[j] = Integer.parseInt(blockSizes[j]);
			for(int j=0;j < qStarts.length;j++) align.qStarts[j] = Integer.parseInt(qStarts[j]);
			for(int j=0;j < tStarts.length;j++) align.tStarts[j] = Integer.parseInt(tStarts[j]);

			align.strand[0]= l[8].charAt(0);
			align.strand[1]= ' '; 
			if (l[8].length() > 1){
				align.strand[0]= l[8].charAt(1);
				align.qsize=align.qsize*3;
				align.qstart=align.qstart*3;
				align.qend=align.qend*3;
				align.matches=align.matches*3;
				for(int i=0;i<blockCount;i++){
					align.blockSizes[i]=(align.blockSizes[i])*3;
					align.qStarts[i]=align.qStarts[i]*3;
				}
				if(align.strand[0]=='-'){
					int[] bs=new int[blockCount];
					int[] qs=new int[blockCount];
					int[] ts=new int[blockCount];
					for(int i=0;i<blockCount;i++){
						bs[i]=align.blockSizes[blockCount-1-i];
						qs[i]=align.qsize-bs[i]-align.qStarts[blockCount-1-i];
						ts[i]=align.tsize-bs[i]-align.tStarts[blockCount-1-i];
					}
					for(int i=0;i<blockCount;i++){
						align.blockSizes[i]=bs[i];
						align.qStarts[i]=qs[i];
						align.tStarts[i]=ts[i];
					}
				}
			}
			
			curralign = align;
			hasmoreseq= true;
		}
	}

	public static void main(String[] args) throws IOException, Exception {
		BufferedReader reader= new BufferedReader(new FileReader("/tmp/refseqblat.psl"));
		PslParser parser= new PslParser(reader);
		while (parser.hasMoreAlignments()) {
			EstAlignment a= parser.nextAlignment();
			System.out.println(a);
		}
		
	}

	/* (non-Javadoc)
	 * @see java.util.Iterator#remove()
	 */
	public void remove() {
		throw new UnsupportedOperationException();
	}

}
